package com.ego.dubbo.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.dubbo.service.TbItemParamDubboService;
import com.ego.mapper.TbItemParamMapper;
import com.ego.pojo.TbItemParam;
import com.ego.pojo.TbItemParamExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@DubboService
public class TbItemParamDubboServiceImlp implements TbItemParamDubboService {
    @Autowired
    private TbItemParamMapper tbItemParamMapper;

    @Override
    public PageInfo<TbItemParam> selectTbItemParamByPage(int page, int size) {
        //开始分页
        PageHelper.startPage(page,size);

        TbItemParamExample tbItemParamExample = new TbItemParamExample();

        List<TbItemParam> tbItemParams = tbItemParamMapper.selectByExampleWithBLOBs(tbItemParamExample);

        PageInfo<TbItemParam> paramPageInfo=new PageInfo<>(tbItemParams);
        return paramPageInfo;
    }

    @Override
    public TbItemParam selectByItemCatId(long itemCatId) {
        TbItemParamExample tbItemParamExample = new TbItemParamExample();
        tbItemParamExample.createCriteria().andItemCatIdEqualTo(itemCatId);

        List<TbItemParam> tbItemParams = tbItemParamMapper.selectByExampleWithBLOBs(tbItemParamExample);

        return tbItemParams.size()==1?tbItemParams.get(0):null;
    }

    @Override
    public int insert(TbItemParam tbItemParam) {

        return tbItemParamMapper.insert(tbItemParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(List<Long> ids) throws DaoException {
        TbItemParamExample tbItemParamExample = new TbItemParamExample();
        tbItemParamExample.createCriteria().andIdIn(ids);
        try {
            int i = tbItemParamMapper.deleteByExample(tbItemParamExample);
            if (i==ids.size()){
                return 1;
            }else{
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new DaoException("删除失败");
        }

    }
}
